package com.antler.luajavalibrary.luaext.ui;

import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.antler.luajavalibrary.luaext.utils.ColorUtils;
import com.antler.luajavalibrary.luaext.utils.DensityUtils;
import com.antler.luajavalibrary.luaext.utils.FastJSONUtils;

public class UIDiv extends LinearLayout implements UIView {
    protected static final String TAG = "antler_" + UIDiv.class.getSimpleName();

    public static final String DEFAULT_BACKGROUND_COLOR = "#FFFFFF";

    protected final UIContext mUIContext;

    protected String mId;

    public UIDiv(UIContext uiContext, String id, JSONObject parser) {
        super(uiContext.getContext());

        mUIContext = uiContext;

        mId = id;

        if (parser != null) {
            setAttr(parser);

            String className = parser.getString("class");
            if (className != null) {
                setStyle(mUIContext.getGlobalStyle(className));
            }

            setStyle(parser.getJSONObject("style"));
        }
    }

    @Override
    public String getID() {
        return mId;
    }

    @Override
    public void setID(String id) {
        mId = id;
    }

    @Override
    public int getType() {
        return UI.Type.DIV;
    }

    @Override
    public int subviewsCount() {
        return 0;
    }

    @Override
    public UIView getSubview(int index) {
        return null;
    }

    @Override
    public void addSubView(UIView subview, int index) {

    }

    @Override
    public void removeSubview(int index) {

    }

    @Override
    public void removeFromParent() {

    }

    @Override
    public void setActionCallback(int action, int funID) {
        Log.v(TAG, "action=" + action + ", funID=" + funID);
    }

    @Override
    public void removeAllActionCallback() {

    }

    @Override
    public String toJson() {
        return null;
    }

    @Override
    public void setAttr(String attrs) {
        final JSONObject parser = JSON.parseObject(attrs);
        if (parser != null) {
            setAttr(parser);
        }
    }

    @Override
    public void setStyle(String styles) {
        final JSONObject parser = JSON.parseObject(styles);
        if (parser != null) {
            setStyle(parser);
        }
    }

    @Override
    public void setPseudoStyle(String pseudo, String styles) {
        final JSONObject parser = JSON.parseObject(styles);
        if (parser != null) {
            setPseudoStyle(pseudo, parser);
        }
    }

    protected void setAttr(JSONObject attrs) {

    }

    protected void setStyle(JSONObject styles) {

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
        if (params == null) {
            params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        }

        int width = FastJSONUtils.getIntValue(styles, "width", UI.LayoutParams.DEFAULT_WIDTH);
        int height = FastJSONUtils.getIntValue(styles, "height", ViewGroup.LayoutParams.WRAP_CONTENT);
        params.width = width;
        params.height = height;

        // 内边距
        int padding = FastJSONUtils.getIntValue(styles, "padding", 0);
        int paddingLeft = FastJSONUtils.getIntValue(styles, "padding-left", 0);
        int paddingTop = FastJSONUtils.getIntValue(styles, "padding-top", 0);
        int paddingRight = FastJSONUtils.getIntValue(styles, "padding-right", 0);
        int paddingBottom = FastJSONUtils.getIntValue(styles, "padding-bottom", 0);

        paddingLeft = DensityUtils.px2dip(getContext(), paddingLeft == 0 ? padding : paddingLeft);
        paddingTop = DensityUtils.px2dip(getContext(), paddingTop == 0 ? padding : paddingTop);
        paddingRight = DensityUtils.px2dip(getContext(), paddingRight == 0 ? padding : paddingRight);
        paddingBottom = DensityUtils.px2dip(getContext(), paddingBottom == 0 ? padding : paddingBottom);
        setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);

        // 外边距
        int margin = FastJSONUtils.getIntValue(styles, "margin", 0);
        int marginLeft = FastJSONUtils.getIntValue(styles, "margin-left", 0);
        int marginTop = FastJSONUtils.getIntValue(styles, "margin-top", 0);
        int marginRight = FastJSONUtils.getIntValue(styles, "margin-right", 0);
        int marginBottom = FastJSONUtils.getIntValue(styles, "margin-bottom", 0);

        marginLeft = DensityUtils.px2dip(getContext(), marginLeft == 0 ? margin : marginLeft);
        marginTop = DensityUtils.px2dip(getContext(), marginTop == 0 ? margin : marginTop);
        marginRight = DensityUtils.px2dip(getContext(), marginRight == 0 ? margin : marginRight);
        marginBottom = DensityUtils.px2dip(getContext(), marginBottom == 0 ? margin : marginBottom);
        params.setMargins(marginLeft, marginTop, marginRight, marginBottom);

        // 设定边框样式
        String borderStyle = FastJSONUtils.getString(styles, "border-style", "solid");
        String borderLeftStyle = FastJSONUtils.getString(styles, "border-left-style", "solid");
        String borderTopStyle = FastJSONUtils.getString(styles, "border-top-style", "solid");
        String borderRightStyle = FastJSONUtils.getString(styles, "border-right-style", "solid");
        String borderBottomStyle = FastJSONUtils.getString(styles, "border-bottom-style", "solid");

        // 设定边框宽度, 非负值, 默认值0
        int borderWidth = FastJSONUtils.getIntValue(styles, "border-width", 0);
        int borderLeftWidth = FastJSONUtils.getIntValue(styles, "border-left-width", 0);
        int borderTopWidth = FastJSONUtils.getIntValue(styles, "border-top-width", 0);
        int borderRightWidth = FastJSONUtils.getIntValue(styles, "border-right-width", 0);
        int borderBottomWidth = FastJSONUtils.getIntValue(styles, "border-bottom-width", 0);

        // 设定边框颜色
        String borderColor = FastJSONUtils.getString(styles, "border-color", "#000000");
        String borderLeftColor = FastJSONUtils.getString(styles, "border-left-color", "#000000");
        String borderTopColor = FastJSONUtils.getString(styles, "border-top-color", "#000000");
        String borderRightColor = FastJSONUtils.getString(styles, "border-right-color", "#000000");
        String borderBottomColor = FastJSONUtils.getString(styles, "border-bottom-color", "#000000");

        // 设定圆角, 默认值0
        int borderRadius = FastJSONUtils.getIntValue(styles, "border-radius", 0);
        int borderBottomLeftRadius = FastJSONUtils.getIntValue(styles, "border-bottom-left-radius", 0);
        int borderBottomRightRadius = FastJSONUtils.getIntValue(styles, "border-bottom-right-radius", 0);
        int borderTopLeftRadius = FastJSONUtils.getIntValue(styles, "border-top-left-radius", 0);
        int borderTopRightRadius = FastJSONUtils.getIntValue(styles, "border-top-right-radius", 0);

        // Flex容器
        String flexDirection = FastJSONUtils.getString(styles, "flex-direction", "column");
        String justifyContent = FastJSONUtils.getString(styles, "justify-content", "flex-start");
        String alignItems = FastJSONUtils.getString(styles, "align-items", "stretch");
        int flex = FastJSONUtils.getIntValue(styles, "flex", 0);

        setOrientation(flexDirection == "column" ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL);

        // Position定位
        String position = FastJSONUtils.getString(styles, "position", "relative");
        int left = FastJSONUtils.getIntValue(styles, "left", 0);
        int top = FastJSONUtils.getIntValue(styles, "top", 0);
        int right = FastJSONUtils.getIntValue(styles, "right", 0);
        int bottom = FastJSONUtils.getIntValue(styles, "bottom", 0);

        setLayoutParams(params);

        ///////////////////////////////////////////
        int color = ColorUtils.parseColor(FastJSONUtils.getString(styles, "background-color", DEFAULT_BACKGROUND_COLOR));
        setBackgroundColor(color);
    }

    protected void setPseudoStyle(String pseudo, JSONObject styles) {

    }
}
